import Vue from 'vue'
import App from './App'
import router from './router'

// import 'element-ui/lib/theme-chalk/index.css'
import './element/index'
import './styles/index.less'

import './permision'
import store from './store/index'
import '@/assets/icon'
import './mock/index'
import api from './api/index'
import './public-path'

import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
Vue.prototype.$api = api

const app = new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
}).$mount('#app')

function handleMicroData() {
  // 是否是微前端环境
  if (window.__MICRO_APP_ENVIRONMENT__) {
    // 主动获取基座下发的数据
    console.log('child-vue2 getData:', window.microApp.getData())

    // 监听基座下发的数据变化
    window.microApp.addDataListener((data) => {
      console.log('child-vue2 addDataListener:', data)

      // 当基座下发path时进行跳转
      if (data.path && data.path !== router.currentRoute.path) {
        router.push(data.path)
      }
    })

    // 向基座发数据
    setTimeout(() => {
      window.microApp.dispatch({ myname: 'vue2+webpack5' })
    }, 3000)

  }
}

handleMicroData()

window.addEventListener('unmount', function () {
  window.microApp.clearDataListener()
  app.$destroy()
})
